package com.example.duckshop.ui.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.duckshop.R;
import com.example.duckshop.bean.User;
import com.example.duckshop.util.MySqliteOpenHelper;
import com.example.duckshop.util.SPUtils;
import com.example.duckshop.widget.ActionBar;

/**
 * 登录页面
 */
public class LoginActivity extends Activity {
    MySqliteOpenHelper helper = null;
    private static final String TAG = "LoginActivity";
    private Activity activity;
    private ActionBar mTitleBar;//标题栏
    private EditText etAccount;//手机号
    private EditText etPassword;//密码
    private TextView tvRegister;//注册
    private Button btnLogin;//登录按钮
    private Button btntelephone;//客服按钮
    private Button btnvideo;//视频按钮
    private RadioGroup rgType;//用户类型

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);//加载页面

        activity = this;
        helper = new MySqliteOpenHelper(this);

        etAccount = (EditText) findViewById(R.id.et_account);//获取手机号
        etPassword = (EditText) findViewById(R.id.et_password);//获取密码
        tvRegister = (TextView) findViewById(R.id.tv_register);//获取注册
        btnLogin = (Button) findViewById(R.id.btn_login);//获取登录
        rgType = findViewById(R.id.rg_type);


        mTitleBar = (ActionBar) findViewById(R.id.myActionBar);
        //setData()方法用于设置ActionBar的数据，包括标题、左侧的图标、右侧的图标、背景颜色等，设置左右图标的点击事件
        mTitleBar.setData(activity, "登录", 0, 0, 0, getResources().getColor(R.color.colorPrimary), new ActionBar.ActionBarClickListener() {
            @Override
            public void onLeftClick() {
                finish();
            }
            //左图标的点击事件,在点击该图标时结束当前的Activity。
            @Override
            public void onRightClick() {
            }
        });

        //注册
        tvRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转到注册页面
                Intent intent = new Intent(activity, RegisterActivity.class);
                startActivity(intent);
            }
        });

        //跳转客服页面
        btntelephone = (Button) findViewById(R.id.btn_telephone);
        btntelephone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(LoginActivity.this,MessageActivity.class);
                startActivity(intent);
            }
        });

        //跳转视频页面
        btnvideo = (Button) findViewById(R.id.btn_video);
        btnvideo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(LoginActivity.this,VideoActivity.class);
                startActivity(intent);
            }
        });

        //设置点击按钮
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //关闭虚拟键盘
                //InputMethodManager inputMethodManager = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                //inputMethodManager.hideSoftInputFromWindow(v.getWindowToken(), 0);
                //获取请求参数
                String account = etAccount.getText().toString();
                String password = etPassword.getText().toString();
                if ("".equals(account)) {//账号不能为空
                    Toast.makeText(activity, "账号不能为空", Toast.LENGTH_LONG).show();
                    return;
                }
                if ("".equals(password)) {//密码为空
                    Toast.makeText(activity, "密码为空", Toast.LENGTH_LONG).show();
                    return;
                }
                User mUser = null;   //定义 User 对象 mUser，初始化为 null
                String sql = "select * from user where account = ?";
                SQLiteDatabase db = helper.getWritableDatabase();
                Cursor cursor = db.rawQuery(sql, new String[]{account});
                //如果查询返回的结果不为空，就对查询结果进行遍历，读取每一项的信息，最后利用这些信息创建一个 User 对象
                //如果查询返回的结果为空，说明该账号不存在，则给用户相应的提示。
                if (cursor != null && cursor.getColumnCount() > 0) {
                    while (cursor.moveToNext()) {
                        Integer dbId = cursor.getInt(0);
                        String dbAccount = cursor.getString(1);
                        String dbPassword = cursor.getString(2);
                        String dbNickName = cursor.getString(3);
                        Integer dbAge = cursor.getInt(4);
                        String dbEmail = cursor.getString(5);
                        mUser = new User(dbId, dbAccount, dbPassword, dbNickName, dbAge, dbEmail);
                    }
                }
                db.close();
                if (mUser != null) {
                    if (!password.equals(mUser.getPassword())) {
                        Toast.makeText(LoginActivity.this, "密码错误", Toast.LENGTH_SHORT).show();
                    } else {
                        SPUtils.put(LoginActivity.this, SPUtils.USER_ID, mUser.getId()); //把当前用户的 ID 存储到 SharedPreferences 中
                        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                        startActivity(intent);
                        finish();
                    }
                } else {
                    Toast.makeText(LoginActivity.this, "账号不存在", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

}
